package dao.impl;

import entity.UserEntity;
import dao.UserDao;
import util.JDBCUtil;

import java.util.List;
import java.util.Map;

public class UserDaoImpl extends JDBCUtil implements UserDao {
    @Override
    public int insert(UserEntity user) {
        String sql = "INSERT INTO user_info (id,username, password) VALUES (?,?,?)";
        return insert(sql, user.getId(), user.getUsername(), user.getPassword());
    }

    @Override
    public UserEntity findByUsername(String username) {
        String sql = "SELECT * FROM user_info WHERE username = ?";
        List<Map<String, Object>> queryForList = queryForList(sql, username);
        if (queryForList.isEmpty()) {
            return null;
        }
        Map<String, Object> objectMap = queryForList.get(0);
        return UserEntity.mapToEntity(objectMap);
    }
}
